package reflection;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Test09 {

    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {

        Class<?> aClass = Class.forName("reflection.User");
        Object o = aClass.newInstance();
        System.out.println(o);
        User oo = (User)o;
        System.out.println(oo);

        Constructor<?> constructor = aClass.getDeclaredConstructor(String.class, int.class, int.class);
        Object sb = constructor.newInstance("sb", 1, 12);
        System.out.println((User) sb);
        User user3 = (User)aClass.newInstance();
        Method setName = aClass.getDeclaredMethod("setName", String.class);
        Object kuangshen = setName.invoke(user3, "kuangshen");
        System.out.println(user3.getName());

        User user4 = (User) aClass.newInstance();
        Field name = aClass.getDeclaredField("name");
        name.setAccessible(true);
        name.set(user4, "sdff");
        System.out.println(user4);

    }

}
